package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MingCardRecord;
import com.ruoyi.system.service.IMingCardRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 打卡记录Controller
 * 
 * @author ruoyi
 * @date 2022-03-21
 */
@RestController
@RequestMapping("/system/record")
public class MingCardRecordController extends BaseController
{
    @Autowired
    private IMingCardRecordService mingCardRecordService;

    /**
     * 查询打卡记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(MingCardRecord mingCardRecord)
    {
        startPage();
        List<MingCardRecord> list = mingCardRecordService.selectMingCardRecordList(mingCardRecord);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/listDay")
    public TableDataInfo listDay(MingCardRecord mingCardRecord)
    {
        startPage();
        List<MingCardRecord> list = mingCardRecordService.selectMingCardRecordListDay(mingCardRecord);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/listWeek")
    public TableDataInfo listWeek(MingCardRecord mingCardRecord)
    {
        startPage();
        List<MingCardRecord> list = mingCardRecordService.selectMingCardRecordListWeek(mingCardRecord);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/listMonth")
    public TableDataInfo listMonth(MingCardRecord mingCardRecord)
    {
        startPage();
        List<MingCardRecord> list = mingCardRecordService.selectMingCardRecordListMonth(mingCardRecord);
        return getDataTable(list);
    }

    /**
     * 导出打卡记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "打卡记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MingCardRecord mingCardRecord)
    {
        List<MingCardRecord> list = mingCardRecordService.selectMingCardRecordList(mingCardRecord);
        ExcelUtil<MingCardRecord> util = new ExcelUtil<MingCardRecord>(MingCardRecord.class);
        util.exportExcel(response, list, "打卡记录数据");
    }

    /**
     * 获取打卡记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mingCardRecordService.selectMingCardRecordById(id));
    }

    /**
     * 新增打卡记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "打卡记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MingCardRecord mingCardRecord)
    {
        MingCardRecord mingCardRecordTmp = new MingCardRecord();
        mingCardRecordTmp.setName(mingCardRecord.getName());
        mingCardRecordTmp.setStartDate(new Date());
        mingCardRecord.setStartDate(new Date());
        List<MingCardRecord> list = mingCardRecordService.selectMingCardRecordList(mingCardRecordTmp);

        if (null != list && list.size() ==1) {
            mingCardRecordTmp = list.get(0);
            mingCardRecordTmp.setStartDate(new Date());
            mingCardRecordTmp.setCarda(mingCardRecord.getCarda());
            mingCardRecordTmp.setCardb(mingCardRecord.getCardb());
            mingCardRecordTmp.setCardc(mingCardRecord.getCardc());
            mingCardRecordTmp.setCardd(mingCardRecord.getCardd());
            mingCardRecordTmp.setCarde(mingCardRecord.getCarde());
            mingCardRecordTmp.setNickname(mingCardRecord.getNickname());
            mingCardRecordTmp.setAvatar(mingCardRecord.getAvatar());
            mingCardRecord = mingCardRecordTmp;
            return toAjax(mingCardRecordService.updateMingCardRecord(mingCardRecord));
        }
        return toAjax(mingCardRecordService.insertMingCardRecord(mingCardRecord));
    }

    /**
     * 修改打卡记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "打卡记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MingCardRecord mingCardRecord)
    {
        return toAjax(mingCardRecordService.updateMingCardRecord(mingCardRecord));
    }

    /**
     * 删除打卡记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "打卡记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mingCardRecordService.deleteMingCardRecordByIds(ids));
    }
}
